package com.niu.core.common.config.executable.initialize;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.niu.core.common.utils.file.FileTools;
import com.niu.core.common.component.context.SpringContext;
import com.niu.core.common.component.context.WebAppEnvs;
import com.niu.core.common.config.GlobalConfig;
import com.niu.core.entity.site.Site;
import com.niu.core.mapper.site.SiteMapper;
import com.niu.core.service.admin.install.IInstallSystemService;
import com.niu.core.service.core.app.tools.SQLScriptRunnerTools;
import com.niu.core.service.core.schedule.ICoreScheduleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Spring Context声明周期，刷新Context后执行
 */
@Component
@Slf4j
public class CoreSpringContextListener implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private ApplicationEventPublisher publisher;

    @Resource
    private SiteMapper siteMapper;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        log.info("当前应用运行环境：" + GlobalConfig.runActive);
        log.info("当前应用运行环境工作目录：" + WebAppEnvs.get());
        if (this.isInit()) {
            // 第一次安装时初始化创建
            //this.initSqlSchema();
            //this.initSqlData();
            // 初始话菜单
            this.initMenu();
            // 初始话计划任务
            this.initSchedule();
            // 创建完成
            this.setCreated();
            Site site = new Site();
            site.setIsinit(1);
            siteMapper.update(site, new QueryWrapper<Site>().eq("site_id", 0));
        }
        // 根据管道命令初始化执行
        PipeCommandHelper.initAppBasicByPipeCommand();
    }

    /**
     * 初始化SQL数据
     */
    private void initSqlData() {
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("sql/niucloud_data.sql");
        try {
            String sqlContent = IOUtils.toString(inputStream, "UTF-8");
            String tablePrefix = GlobalConfig.tablePrefix;
            if (!GlobalConfig.tablePrefix.endsWith("_")) {
                tablePrefix = tablePrefix + "_";
            }
            if (StrUtil.isNotEmpty(sqlContent)) {
                sqlContent = sqlContent.replaceAll("\\{table_prefix\\}", tablePrefix);
                SQLScriptRunnerTools.execScript(sqlContent);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 初始化SQL脚本
     */
    private void initSqlSchema() {
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("sql/niucloud_schema.sql");
        try {
            String sqlContent = IOUtils.toString(inputStream, "UTF-8");
            String tablePrefix = GlobalConfig.tablePrefix;
            if (!GlobalConfig.tablePrefix.endsWith("_")) {
                tablePrefix = tablePrefix + "_";
            }
            if (StrUtil.isNotEmpty(sqlContent)) {
                sqlContent = sqlContent.replaceAll("\\{table_prefix\\}", tablePrefix);
                SQLScriptRunnerTools.execScript(sqlContent);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 初始化菜单
     */
    private void initMenu() {
        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
        int delaySeconds = 30;  // 延迟是为了解决【mybatis-plus-join】的初始化bug，避免mybatis调用错误
        executorService.schedule(() -> {
            try {
                IInstallSystemService installSystemService = SpringContext.bean(IInstallSystemService.class);
                installSystemService.install();
                log.warn("菜单已创建.");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, delaySeconds, TimeUnit.SECONDS);
        executorService.shutdown();
    }

    /**
     * 初始化计划任务
     */
    private void initSchedule() {
        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
        int delaySeconds = 30;  // 延迟是为了解决【mybatis-plus-join】的初始化bug，避免mybatis调用错误
        executorService.schedule(() -> {
            try {
                ICoreScheduleService coreScheduleService = SpringContext.bean(ICoreScheduleService.class);
                coreScheduleService.installSystemSchedule();
                log.warn("初始化计划任务已创建.");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, delaySeconds, TimeUnit.SECONDS);
        executorService.shutdown();
    }

    /**
     * @return
     */
    private boolean isInit() {
        Site site = siteMapper.selectById(0);
        if(site.getIsinit() == 0)
        {
            return true;
        }

        return false;
/*        String initedContent = FileTools.readTempContent(GlobalConfig.applicationName + "/inited");
        if ("init".equals(initedContent)) {
            return true;
        }
        return false;*/
    }

    /**
     * 设置已创建
     */
    private void setCreated() {
        FileTools.writeTempContent(GlobalConfig.applicationName + "/inited", "created");
    }

}
